package com.jsnu.xiaochi.controller;

import com.jsnu.xiaochi.pojo.Food;
import com.jsnu.xiaochi.pojo.User;
import com.jsnu.xiaochi.service.AdminService;
import com.jsnu.xiaochi.service.LoginService;
import com.jsnu.xiaochi.service.MyService;
import com.jsnu.xiaochi.util.GetUUID;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Random;

@Controller
public class MyController {
    @Resource
    MyService myService;
    @Autowired
    LoginService loginService;

    @Resource
    AdminService adminService;
    @Value("${NGINX_SERVER}")
    private String NGINX_SERVER;
    @Value("${NGINX_HTTP}")
    private String NGINX_HTTP;

    @RequiresRoles("user")
    @RequestMapping("my")
    public String showMy(Model model,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("categorys", adminService.getAllCategory());
        model.addAttribute("favorites",myService.getFavorite(user.getUsername()));
        return "my";
    }

    @RequiresPermissions("user:update")
    @RequestMapping("userUpdate")
    @ResponseBody
    public String userUpdate(String username,String url, String telphone,String newPwd, String sex, String address, MultipartFile img, HttpServletRequest req) throws IOException {
        User user = loginService.userLogin(username);
        user.setTelphone(telphone);
        user.setSex(sex);
        user.setAddress(address);
        user.setImg(url);
        if(newPwd!=null){
            user.setPassword(new SimpleHash("md5",newPwd, user.getSalt(),3).toString());
        }
        myService.userUpdate(user);
        if(img!=null){
            img.transferTo(new File(NGINX_SERVER+"\\xiaochi\\userImg\\"+username+".jpg"));
            user.setImg(NGINX_HTTP+"/xiaochi/userImg/"+username+".jpg");
        }
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        session.setAttribute("user",user);
        return "success";
    }

    @RequiresPermissions("food:insert")
    @RequestMapping("insertFood")
    @ResponseBody
    public String insertFood(String name,String category,MultipartFile img,String intro,String status) throws IOException {
        Food food = new Food();
        Random random = new Random();
        char prefix= (char) (random.nextInt(26)+'A');
        String foodId=prefix + GetUUID.getUUID().substring(10,19);
        food.setId(foodId);
        food.setName(name);
        food.setCategory(category);
        food.setIntro(intro);
        food.setStatus(status);
        food.setCommentCount(0);
        food.setScore("0");
        img.transferTo(new File(NGINX_SERVER+"\\xiaochi\\foodImg\\"+foodId+".jpg"));
        food.setFoodImg(NGINX_HTTP+"/xiaochi/foodImg/"+foodId+".jpg");
        food.setCreateTime(new Timestamp(System.currentTimeMillis()));
        System.out.println(food);
        myService.insertFood(food);
        return "success";
    }

}
